package com.zsrt.java.day20161227.zzm.demo3;

import java.text.DecimalFormat;
import java.util.Scanner;

public class HongBao {

	/**
	 * 
	 */
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请问你要发多少钱......");
		double money = sc.nextDouble();
		System.out.println("您的红包要多少人来抢......");
		int num = sc.nextInt();
		double[] arr = hongBao(num,money);
		for(int i=0;i<arr.length;i++){
			System.out.println("第"+(i+1)+"个人抢了"+arr[i]+"元钱");
		}
		sc.close();
	}
	public static double[] hongBao(int num,double money){
		DecimalFormat df = new DecimalFormat("#.##");
		
	    
	  //计算红包的平均值.
	    double a    = money*0.35;
	    String str1 = df.format(a);
	    double avg  = Double.parseDouble(str1);
	
	    double secondMoney  = money;
	    int index  = 0;
	    double mon = 0;
	    //定义每个人抢了多少钱
	    double[] everyMoney = new double[num];
	    
	    for(int i=num;i>0;i--){
	    	if(i==1){
	    		everyMoney[index]=secondMoney;
	    	}else{
	    		while(true){
	    			double m = Math.random()*secondMoney;
	    		    String str  = df.format(m);
	    		    mon  = Double.parseDouble(str);
	    			//当其中有一人的抢到的红包大于剩下红余额的35%时，则要重新随机出新的被抢的钱,直到随机出来的钱大于0小于剩下钱的85%。
	    			if(mon>0 && mon<avg){
	    				
	    				break;
	    			}
	    		}
	    		secondMoney = Double.parseDouble(df.format(secondMoney-mon));
	    		everyMoney[index] = mon;
	    		avg = Double.parseDouble(df.format(secondMoney*0.35));
	    		index++;
	    	}
	    }
		return everyMoney;
	}
}
